package com.example.demo.controller;

import com.example.demo.model.LoginUser;
import com.example.demo.model.TokenUser;
import com.example.demo.service.EmployeeService;
import com.example.demo.tools.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WJ
 * Date: 2023-02-17
 * Time: 21:36
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

//    员工权限时，首页的table数据
    @RequestMapping("/gettablelist")
    public Object getPersonTable(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        TokenUser principal = (TokenUser) authentication.getPrincipal();
        LoginUser user = principal.getUser();
        return employeeService.getPersonTable(user);
    }

//    通过员工id获取手下的老人列表信息
    @RequestMapping("/getolderlist")
    public Object getOlderListByEmployeeId(@RequestParam Integer id){
        return employeeService.getOlderListByEmployeeId(id);
    }

//    在员工管理中的老人管理对话框中删除老人信息
    @RequestMapping("/deleteolderinfo")
    public Object deleteOlderInfoByOlderId(@RequestParam(value = "olderid") Integer olderId){
        return employeeService.deleteOlderInfoByOlderId(olderId);
    }

//    查询所有的员工信息
    @RequestMapping("/getallemployeelist")
    public Object getAllEmployeeList(){
        return employeeService.getAllEmployeeList();
    }

//    变更老人的上属员工
    @RequestMapping("/changeoldertheemployee")
    public Object changeOlderTheEmployee(@RequestParam(value = "employeeid") Integer employeeId,
                                         @RequestParam(value = "olderid") Integer olderId){
        return employeeService.changeOlderTheEmployee(employeeId,olderId);
    }

//    添加员工
    @RequestMapping("/addemployee")
    public Object addEmployee(String photo_url,
                              @RequestParam String username,
                              @RequestParam String sex,
                              @RequestParam Integer age,
                              @RequestParam String address,
                              @RequestParam String phone,
                              @RequestParam String area,
                              @RequestParam String id_card,
                              @RequestParam Integer admin_id){

        return employeeService.addEmployee(username,sex,age,address,phone,area,id_card,admin_id,photo_url);
    }

//    根据条件查询（员工模块的查询）
    @RequestMapping("/getallemployeebylike")
    public Object getAllEmployeeInfoByUsernameOrAgeD(String username,
                                                  String phone,
                                                  String sex,
                                                  Integer age){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        TokenUser principal = (TokenUser) authentication.getPrincipal();
        LoginUser user = principal.getUser();
        return employeeService.getAllEmployeeInfoByUsernameOrAgeD(user.getId(),username,phone,sex,age);
    }

//    活动管理下的活动审批模块里的查询:查询所有活动信息
    @RequestMapping("/getallactiveinfobyitem")
    public Object getAllActiveInfoByItems(String start_time,
                                          String end_time,
                                          String active_name,
                                          Integer applicant){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        TokenUser principal = (TokenUser) authentication.getPrincipal();
        LoginUser user = principal.getUser();
        return employeeService.getAllActiveInfoByItems(user.getId(),start_time,end_time,active_name,applicant);
    }

//    查询所有活动详情(可以通过 开始时间 结束时间 的范围查找,也可以根据 active_name 来模糊查询)
    @RequestMapping("/getallactiveforactivedetail")
    public Object getAllActiveInfoInActiveDetail(String begin_start_time,
                                                 String begin_end_time,
                                                 String finish_start_time,
                                                 String finish_end_time,
                                                 String active_name){

        return employeeService.getAllActiveInfoInActiveDetail(begin_start_time,begin_end_time,finish_start_time,finish_end_time,active_name);
    }

//    查询所有员工信息(person 和 employee_detail两表联查 : 通过 grade 排序)
    @RequestMapping("/getallemployeeinfoinperonandemployeedetail")
    public Object getAllEmployeeInfo(){
        return employeeService.getAllEmployeeInfo();
    }

//    申请新的活动
    @RequestMapping("/addActiveOnEmployee")
    public Object addActiveOnEmployee(@RequestParam String active_name,
                                      @RequestParam String active_address,
                                      @RequestParam String start_time,
                                      @RequestParam String start_time_h,
                                      @RequestParam String end_time,
                                      @RequestParam String end_time_h,
                                      @RequestParam String context,
                                      @RequestParam String summary,
                                      @RequestParam Integer employee_id,
                                      @RequestParam Integer admin_id,
                                      @RequestParam String p1,
                                      @RequestParam String p2){

        start_time = start_time.substring(0, 10)+start_time_h.substring(10,24);
        end_time = end_time.substring(0, 10)+end_time_h.substring(10,24);

        return employeeService.addActiveOnEmployee(active_name,active_address,start_time,end_time,context,summary,employee_id,admin_id,p1,p2);
    }

//    获取当前登录员工的所有申请活动列表
    @RequestMapping("/getactiveoncurrentemployeeid")
    public Object getActiveOnCurrentEmployeeId(@RequestParam Integer id){
        return employeeService.getActiveOnCurrentEmployeeId(id);
    }

//    对报名参加活动的老人进行审批
    @RequestMapping("/olderjoinactiveprove")
    public Object olderJoinActiveProve(@RequestParam Integer id,
                                       @RequestParam Integer flag){

        if(flag == 1){
            // 通过
            return employeeService.olderJoinActiveProveOnAllow(id);
        }else{
            // 拒绝
            return employeeService.olderJoinActiveProveOnrefuse(id);
        }
    }
}
